# -*- coding: utf-8 -*-
"""
社交互动模块 - 数据模型
"""

from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator
from django.conf import settings


class ActivityComment(models.Model):
    """
    活动评论模型
    
    用户对活动的评论和反馈
    """
    
    activity = models.ForeignKey(
        'activities.Activity',
        on_delete=models.CASCADE,
        related_name='comments',
        verbose_name='活动'
    )
    
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='comments',
        verbose_name='评论用户'
    )
    
    content = models.TextField(
        verbose_name='评论内容'
    )
    
    # 评论图片（支持多张图片，这里简化为单张，实际可建立关联表）
    images = models.CharField(
        max_length=500,
        blank=True,
        verbose_name='图片',
        help_text='图片URL列表，多个URL用逗号分隔'
    )
    
    # 父评论（用于实现评论回复功能）
    parent = models.ForeignKey(
        'self',
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name='replies',
        verbose_name='父评论',
        help_text='回复的评论，为空则是顶级评论'
    )
    
    # 点赞数
    like_count = models.IntegerField(
        default=0,
        verbose_name='点赞数'
    )
    
    created_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name='评论时间'
    )
    
    updated_at = models.DateTimeField(
        auto_now=True,
        verbose_name='更新时间'
    )
    
    class Meta:
        db_table = 'activity_comments'
        verbose_name = '活动评论'
        verbose_name_plural = '活动评论'
        ordering = ['-created_at']
        indexes = [
            models.Index(fields=['activity', '-created_at']),
        ]
    
    def __str__(self):
        return f"{self.user.username} 评论 {self.activity.title}: {self.content[:30]}"


class CommentLike(models.Model):
    """
    评论点赞模型
    
    记录用户对评论的点赞
    """
    
    comment = models.ForeignKey(
        ActivityComment,
        on_delete=models.CASCADE,
        related_name='likes',
        verbose_name='评论'
    )
    
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='comment_likes',
        verbose_name='点赞用户'
    )
    
    created_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name='点赞时间'
    )
    
    class Meta:
        db_table = 'comment_likes'
        verbose_name = '评论点赞'
        verbose_name_plural = '评论点赞'
        unique_together = ('comment', 'user')
        ordering = ['-created_at']
    
    def __str__(self):
        return f"{self.user.username} 点赞了评论"


class ActivityLike(models.Model):
    """
    活动点赞模型
    
    记录用户对活动的点赞
    """
    
    activity = models.ForeignKey(
        'activities.Activity',
        on_delete=models.CASCADE,
        related_name='activity_likes',
        verbose_name='活动'
    )
    
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='activity_likes',
        verbose_name='点赞用户'
    )
    
    created_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name='点赞时间'
    )
    
    class Meta:
        db_table = 'activity_likes'
        verbose_name = '活动点赞'
        verbose_name_plural = '活动点赞'
        unique_together = ('activity', 'user')
        ordering = ['-created_at']
    
    def __str__(self):
        return f"{self.user.username} 点赞了 {self.activity.title}"


class ActivityRating(models.Model):
    """
    活动评分模型
    
    用户对参加过的活动进行1-5星评分
    """
    
    activity = models.ForeignKey(
        'activities.Activity',
        on_delete=models.CASCADE,
        related_name='ratings',
        verbose_name='活动'
    )
    
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='ratings',
        verbose_name='评分用户'
    )
    
    # 评分（1-5星）
    score = models.IntegerField(
        validators=[MinValueValidator(1), MaxValueValidator(5)],
        verbose_name='评分',
        help_text='评分范围：1-5星'
    )
    
    # 评价维度（可选）
    organization_score = models.IntegerField(
        validators=[MinValueValidator(1), MaxValueValidator(5)],
        null=True,
        blank=True,
        verbose_name='组织评分',
        help_text='活动组织质量评分'
    )
    
    experience_score = models.IntegerField(
        validators=[MinValueValidator(1), MaxValueValidator(5)],
        null=True,
        blank=True,
        verbose_name='体验评分',
        help_text='活动体验感评分'
    )
    
    # 评价内容（可选）
    review = models.TextField(
        blank=True,
        verbose_name='评价内容',
        help_text='详细的评价内容'
    )
    
    created_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name='评分时间'
    )
    
    updated_at = models.DateTimeField(
        auto_now=True,
        verbose_name='更新时间'
    )
    
    class Meta:
        db_table = 'activity_ratings'
        verbose_name = '活动评分'
        verbose_name_plural = '活动评分'
        unique_together = ('activity', 'user')
        ordering = ['-created_at']
    
    def __str__(self):
        return f"{self.user.username} 给 {self.activity.title} 评分 {self.score}星"


class ActivityDiscussion(models.Model):
    """
    活动讨论模型
    
    活动专属讨论区，参与者可以分享心得、照片、建议等
    """
    
    activity = models.ForeignKey(
        'activities.Activity',
        on_delete=models.CASCADE,
        related_name='discussions',
        verbose_name='活动'
    )
    
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='discussions',
        verbose_name='发帖用户'
    )
    
    title = models.CharField(
        max_length=200,
        verbose_name='讨论标题'
    )
    
    content = models.TextField(
        verbose_name='讨论内容'
    )
    
    # 图片附件
    images = models.CharField(
        max_length=500,
        blank=True,
        verbose_name='图片',
        help_text='图片URL列表，多个URL用逗号分隔'
    )
    
    # 是否置顶
    is_pinned = models.BooleanField(
        default=False,
        verbose_name='是否置顶'
    )
    
    # 统计字段
    view_count = models.IntegerField(
        default=0,
        verbose_name='浏览次数'
    )
    
    reply_count = models.IntegerField(
        default=0,
        verbose_name='回复数'
    )
    
    like_count = models.IntegerField(
        default=0,
        verbose_name='点赞数'
    )
    
    created_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name='发布时间'
    )
    
    updated_at = models.DateTimeField(
        auto_now=True,
        verbose_name='更新时间'
    )
    
    class Meta:
        db_table = 'activity_discussions'
        verbose_name = '活动讨论'
        verbose_name_plural = '活动讨论'
        ordering = ['-is_pinned', '-created_at']
        indexes = [
            models.Index(fields=['activity', '-created_at']),
        ]
    
    def __str__(self):
        return f"{self.user.username} 在 {self.activity.title} 发起讨论: {self.title}"


class DiscussionReply(models.Model):
    """
    讨论回复模型
    
    用户对讨论帖的回复
    """
    
    discussion = models.ForeignKey(
        ActivityDiscussion,
        on_delete=models.CASCADE,
        related_name='replies',
        verbose_name='讨论帖'
    )
    
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='discussion_replies',
        verbose_name='回复用户'
    )
    
    content = models.TextField(
        verbose_name='回复内容'
    )
    
    # 回复的是哪条回复（用于实现楼中楼）
    parent_reply = models.ForeignKey(
        'self',
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name='sub_replies',
        verbose_name='父回复'
    )
    
    created_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name='回复时间'
    )
    
    class Meta:
        db_table = 'discussion_replies'
        verbose_name = '讨论回复'
        verbose_name_plural = '讨论回复'
        ordering = ['created_at']
    
    def __str__(self):
        return f"{self.user.username} 回复了讨论"
